﻿using Raffles.OA.Utility;
using Raffles.OA.Data;
using Mapster;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.Areas.Manage.Controllers
{
    /// <summary>
    /// 日 期：2023-12-25 13:54:41
    /// 描 述：编号规则控制器类
    /// </summary>
    public class NumberRuleController : BaseController<NumberRule>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public ActionResult GetPageListJson(BaseSearchParam param, Pagination pagination)
        {
            int totalCount = 0;
            var datas = _dbContext.Queryable<NumberRule>()
                                /*.WhereIF(!param.KeyWord.IsEmpty(), x => x.Name == param.KeyWord.Trim())*/
                                .ToPageList(pagination.PageIndex, pagination.PageSize, ref totalCount);
            return Content(DataResult<List<NumberRule>>.PageResult(datas, totalCount).ToJson());
        }

        /// <summary>
        /// Form表单
        /// </summary>
        /// <returns></returns>
        public ActionResult Form()
        {
            return View();
        }

        /// <summary>
        /// 表单保存
        /// </summary>
        /// <param name="numberRuleDto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveForm(NumberRuleDto numberRuleDto)
        {
            try
            {
                NumberRule entity = new NumberRule();
                if (numberRuleDto.Id == null)
                {
                    numberRuleDto.Adapt(entity); 
                    entity.CreatedOn = DateTime.Now;
                    entity.CreatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Insertable<NumberRule>(entity).ExecuteCommand();
                    return Json(DataResult.Success("新增成功"));
                }
                else
                {
                    entity = _dbContext.Queryable<NumberRule>().InSingle(numberRuleDto.Id);
                    numberRuleDto.Adapt(entity); 
                    entity.UpdatedOn = DateTime.Now;
                    entity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Updateable<NumberRule>(entity).ExecuteCommand();
                    return Json(DataResult.Success("编辑成功"));
                }
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"保存失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteData(int id)
        {
            try
            {
                _dbContext.Deleteable<NumberRule>().In(id).ExecuteCommand();
                return Json(DataResult.Success("删除成功"));
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"删除失败,失败原因:{ex.Message}"));
            }
        }
    }
}